<?php
namespace app\super\controller;

use think\Request;
use think\facade\Config;
use think\Db;

class Admin extends \Common {

    # 登录
    public function Login(){
        # bidco 代拍公司登录后台 | super 超级管理登录后台 | sub 次级管理员 | ctrl 次级管理员 | ctrlbid  次级管理员
        if(isset($_POST["username"]) && isset($_POST["password"]) && isset($_POST["role"])){
            $username = $_POST["username"];
            $password = $_POST["password"];
            $role = $_POST["role"];
            switch ($role){
                case "sub" :
                    $admin = Db::name("admin")->where(function ($query){
                        $query->where("role", 2)->whereOr("role", 0);
                    })->where("username", $username)->where("password", $password)->find();
                    break;
                case "ctrl" : case "ctrlbid" : case "aucer" :
                $admin = Db::name("admin")
                    ->where("role", 2)
                    ->where("username", $username)
                    ->where("password", $password)
                    ->find();
                break;
                case "bidco" :
                    $admin = Db::name("user_proxy")
                        ->where("username", $username)
                        ->where("password", $password)
                        ->find();
                    break;
                default :
                    $admin = Db::name("admin")
                        ->where("role", 1)
                        ->where("username", $username)
                        ->where("password", $password)
                        ->find();
            }
            if(!empty($admin)){
                $roles = ["admin"];
                if(isset($admin["roles"]) && $admin["roles"] != 0){
                    switch ($admin["roles"]){
                        case "1" : $roles = ["editor"]; break;
                        case "2" : $roles = ["outer"]; break;
                        case "3" : $roles = ["cashier"]; break;
                        case "4" : $roles = ["auditor"]; break;
                    }
                }
                if(isset($admin["roles"]) && $admin["roles"] != 0){
                    $arr = [
                        "aid" => $admin["aid"],
                        "pid" => $admin["pid"],
                        "table" => "admin",
                        "time" => time()
                    ];
                }else if(isset($admin["roles"])){
                    $arr = [
                        "aid" => $admin["aid"],
                        "table" => "admin",
                        "time" => time()
                    ];
                }else{
                    $arr = [
                        "aid" => $admin["up_id"],
                        "table" => "user_proxy",
                        "time" => time()
                    ];
                }
                $token = $this->MakeToken($arr);
                $array = [
                    "token" => $token,
                    "roles" => $roles
                ];
                   Code(200, "登录成功", $array);
            }else{
                Code(403, "账号密码错误");
            }
        }else{
            Code(403, "服务器错误，请稍后再试");
        }
    }

    # 获取信息
    public function Info(){
        $aid = $this->token["id"]; $table = $this->token["table"];
        if($table == "user_proxy"){
            $admin = Db::name("user_proxy")->where("up_id", $aid)->find();
            $number = 0;
        }else{
            $admin = Db::name("admin")->where("aid", $aid)->find();
            $number = Db::name('num')->where("aid", $aid)->order('number desc')->find();
        }
        $roles = ["admin"];
        if(isset($admin["roles"]) && $admin["roles"] != 0){
            switch ($admin["roles"]){
                case "1" : $roles = ["editor"]; break;
                case "2" : $roles = ["outer"]; break;
                case "3" : $roles = ["cashier"]; break;
                case "4" : $roles = ["auditor"]; break;
            }
        }
        if(isset($admin["roles"]) && $admin["roles"] == 1){
            $array = [
                "roles" => $roles,
                "avatar" => Config::get("app.ip").'static/image/admin.jpg',
                "name" => $admin["name"],
                "username" => $admin["username"]
            ];
        }else{
            $array = [
                "roles" => $roles,
                "avatar" => Config::get("app.ip").'static/image/admin.jpg',
                "name" => $admin["name"],
                "username" => $admin["username"],
                "max" => empty($number) ? 0 : (int)$number['number']
            ];
        }
        Code(200, false, $array);
    }

    # 注销
    public function Logout(){
        Code(200, false, "success");
    }

    # 获取次级管理员列表
    public function GetSubs(){
        $admin = Db::name("admin")->where("role", 2)->order("aid desc")->select();
        $arr = [];
        if(!empty($admin)){
            foreach ($admin as $value){
                $arr[] = [
                    "id" => (int)$value["aid"],
                    "name" => $value["name"],
                    "account" => $value["username"],
                    "password" => base64_decode($value["password64"])
                ];
            }
        }

        Code(200, false, $arr);
    }

    # 添加、修改次级管理员
    public function UpdateSub(){
        if(isset($_POST["name"]) && isset($_POST["account"]) && isset($_POST["password"])){
            $name = $_POST["name"];
            $account = $_POST["account"];
            $password = $_POST["password"];
            $info = [
                "name" => $name,
                "username" => $account,
                "password" => md5($password),
                "password64" => base64_encode($password),
                "role" => 2
            ];
            if(isset($_POST["id"])){
                $id = $_POST["id"];
                $admin = Db::name("admin")->where("role", 2)->where("username", $account)->where("aid != $id")->find();
                if(empty($admin)){
                    Db::name("admin")->where("aid", $id)->update($info);
                    $array = [
                        "id" => (int)$id,
                        "account" => $account,
                        "name" => $name,
                        "password" => $password,
                    ];
                    Code(200, "账号修改成功", $array);
                }else{
                    Code(403, "账号已存在");
                }
            }else{
                $admin = Db::name("admin")->where("role", 2)->where("username", $account)->find();
                if(empty($admin)){
                    $insertID = Db::name("admin")->insertGetId($info);
                    $array = [
                        "id" => (int)$insertID,
                        "account" => $account,
                        "name" => $name,
                        "password" => $password,
                    ];
                    Code(200, "账号添加成功", $array);
                }else{
                    Code(403, "账号已存在");
                }
            }
        }else{
            Code(403, "服务器错误，请稍后再试");
        }
    }

    # 获取代拍公司人员列表
    public function GetBidCos(){
        $id = $this->token["id"];
        $aid = $aid = isset($this->token["pid"]) ? $this->token["pid"] : $this->token["id"];
        if(isset($_GET["page"]) && isset($_GET["limit"]) && isset($_GET["search_key"]) && isset($_GET["search_val"])){
            $page = $_GET["page"]; $limit = $_GET["limit"]; $key = $_GET["search_key"]; $val = $_GET["search_val"];
            $role = Db::name("admin")->where("aid", $id)->find();

            if($key == "name" && $val != ""){
                if($role["role"] == 1){
                    $proxy = Db::name("user_proxy")->where("name", $val)->order("up_id desc")->select();
                    $count = Db::name("user_proxy")->where("name", $val)->count();
                }else{
                    $proxy = Db::name("user_proxy_company")->alias("a")
                        ->join("c_user_proxy b", "a.up_id = b.up_id")
                        ->where("aid", $aid)
                        ->where("b.name like '%{$_GET["search_val"]}%'")
                        ->page($page, $limit)
                        ->select();
                    $count = Db::name("user_proxy_company")->alias("a")
                        ->join("c_user_proxy b", "a.up_id = b.up_id")
                        ->where("aid", $aid)
                        ->where("b.name like '%{$_GET["search_val"]}%'")
                        ->count();
                    if(empty($proxy)){
                        $proxy = Db::name("user_proxy")
                            ->where("name like '%{$_GET["search_val"]}%'")
                            ->page($page, $limit)
                            ->select();
                        $count = Db::name("user_proxy")
                            ->where("name like '%{$_GET["search_val"]}%'")
                            ->count();
                    }
                }
            }else if($key == "tel" && $val != ""){
                if($role["role"] == 1){
                    $proxy = Db::name("user_proxy")->where("tel", $val)->order("up_id desc")->select();
                    $count = Db::name("user_proxy")->where("tel", $val)->count();
                }else{
                    $proxy = Db::name("user_proxy_company")->alias("a")
                        ->join("c_user_proxy b", "a.up_id = b.up_id")
                        ->where("aid", $aid)
                        ->where("b.tel", $_GET["search_val"])
                        ->page($page, $limit)
                        ->select();
                    $count = Db::name("user_proxy_company")->alias("a")
                        ->join("c_user_proxy b", "a.up_id = b.up_id")
                        ->where("aid", $aid)
                        ->where("b.tel", $_GET["search_val"])
                        ->count();
                    if(empty($proxy)){
                        $proxy = Db::name("user_proxy")
                            ->where("tel", $_GET["search_val"])
                            ->page($page, $limit)
                            ->select();
                        $count = Db::name("user_proxy")
                            ->where("tel", $_GET["search_val"])
                            ->count();
                    }
                }
            }else{
                if($role["role"] == 1){
                    $proxy = Db::name("user_proxy")->order("up_id desc")->select();
                    $count = Db::name("user_proxy")->count();
                }else{
                    $proxy = Db::name("user_proxy_company")->alias("a")
                        ->where("aid", $aid)
                        ->join("c_user_proxy b", "a.up_id = b.up_id")
                        ->page($page, $limit)
                        ->select();
                    $count = Db::name("user_proxy_company")->alias("a")
                        ->where("aid", $aid)
                        ->join("c_user_proxy b", "a.up_id = b.up_id")
                        ->count();
                }
            }

            $list = [];
            if($role["role"] == 1){
                foreach ($proxy as $value){
                    $list[] = [
                        "base" => [
                            "id" => $value["up_id"],
                            "address" => $value["address"],
                            "bankcard" => $value["bankcard"],
                            "bankname" => $value["bankname"],
                            "email" => $value["email"],
                            "line" => $value["line"],
                            "name" => $value["name"],
                            "username" => $value["username"],
                            "password" => base64_decode($value["password64"]),
                            "postcode" => $value["postcode"],
                            "tel" => $value["tel"],
                            "wxid" => $value["wxid"],
                        ],
                        "buy" => [
                            "id" => "null-".$value["up_id"],
                            "subid" => "null-".$value["up_id"],
                            "buy_level" => 0,
                            "credit_level" => 0,
                            "judg_level" => 0,
                            "fee_rate" => 0,
                            "remark" => "",
                        ]
                    ];
                }
            }else if(($role["role"] == 2) || ($role["role"] == 0 && $role["roles"] == 3) || ($role["role"] == 0 && $role["roles"] == 4)){
                foreach ($proxy as $value){
                    $list[] = [
                        "base" => [
                            "id" => $value["up_id"],
                            "address" => $value["address"],
                            "bankcard" => $value["bankcard"],
                            "bankname" => $value["bankname"],
                            "email" => $value["email"],
                            "line" => $value["line"],
                            "name" => $value["name"],
                            "postcode" => $value["postcode"],
                            "tel" => $value["tel"],
                            "wxid" => $value["wxid"],
                        ],
                        "buy" => isset($value["upc_id"]) ? [
                            "id" => $value["upc_id"],
                            "subid" => $value["up_id"],
                            "buy_level" => $value["buy_level"],
                            "credit_level" => $value["credit_level"],
                            "judg_level" => $value["judg_level"],
                            "fee_rate" => (float)$value["fee_rate"] * 10000 / 1000000,
                            "remark" => $value["remark"],
                        ] : [
                            "id" => "null-".$value["up_id"],
                            "subid" => "null-".$value["up_id"],
                            "buy_level" => 0,
                            "credit_level" => 0,
                            "judg_level" => 0,
                            "fee_rate" => 0,
                            "remark" => "",
                        ]
                    ];
                }
            }else{
                Code(403, "对不起，您没有权限");
            }
            Code(200, false, ["list" => $list, "total" => $count]);
        }else{
            Code(403, "服务器错误，请稍后再试");
        }
    }

    # 添加、修改代拍公司人员
    public function UpdateBidCo(){
        $id = $this->token["id"];
        $aid = $aid = isset($this->token["pid"]) ? $this->token["pid"] : $this->token["id"];
        if(isset($_POST["base"]) && isset($_POST["buy"])){
            $base = json_decode($_POST["base"], true); $buy = json_decode($_POST["buy"], true);
            $role = Db::name("admin")->where("aid", $id)->find();

            # base
            $address = $base["address"]; $bankcard = $base["bankcard"];
            $bankname = $base["bankname"]; $email = $base["email"]; $line = $base["line"];
            $name = $base["name"]; $postcode = $base["postcode"]; $tel = $base["tel"];
            $wxid = $base["wxid"];

            # buy
            $buy_level = $buy["buy_level"]; $credit_level = $buy["credit_level"]; $judg_level = $buy["judg_level"];
            $fee_rate = $buy["fee_rate"]; $remark = $buy["remark"];

            if($role["role"] == 1){
                $username = $base["username"]; $password = $base["password"];
                # 修改
                if(isset($base["id"])){
                    $up_id = $base["id"];
                    $check = Db::name("user_proxy")->where("up_id != $up_id")->where(function ($query) use ($username, $tel){
                        $query->where("username", $username)->whereOr("tel", $tel);
                    })->find();
                    if(empty($check)){
                        $base_info = [
                            "username" => $username,
                            "password" => md5($password),
                            "password64" => base64_encode($password),
                            "name" => $name,
                            "tel" => $tel,
                            "wxid" => $wxid,
                            "email" => $email,
                            "line" => $line,
                            "postcode" => $postcode,
                            "address" => $address,
                            "bankname" => $bankname,
                            "bankcard" => $bankcard,
                        ];
                        Db::name("user_proxy")->where("up_id", $up_id)->update($base_info);
                        Code(200, "代拍信息修改成功");
                    }else{
                        Code(403, "代拍用户名或者电话号码重复");
                    }
                }else{
                    # 添加
                    $check = Db::name("user_proxy")->where(function ($query) use ($username, $tel){
                        $query->where("username", $username)->whereOr("tel", $tel);
                    })->find();
                    if(empty($check)){
                        $base_info = [
                            "username" => $username,
                            "password" => md5($password),
                            "password64" => base64_encode($password),
                            "name" => $name,
                            "tel" => $tel,
                            "wxid" => $wxid,
                            "email" => $email,
                            "line" => $line,
                            "postcode" => $postcode,
                            "address" => $address,
                            "bankname" => $bankname,
                            "bankcard" => $bankcard,
                        ];
                        Db::name("user_proxy")->insert($base_info);
                        Code(200, "代拍添加成功");
                    }else{
                        Code(403, "代拍用户名或者电话号码重复");
                    }
                }
            }else{
                $upc_id = $buy["id"]; $subid = $buy["subid"]; $up_id = $base["id"];
                $check = Db::name("user_proxy")->where("up_id != $up_id")->where("tel", $tel)->find();
                if(!empty($check)){
                    Code(403, "代拍电话号码重复");
                }else{
                    $base_info = [
                        "name" => $name,
                        "tel" => $tel,
                        "wxid" => $wxid,
                        "email" => $email,
                        "line" => $line,
                        "postcode" => $postcode,
                        "address" => $address,
                        "bankname" => $bankname,
                        "bankcard" => $bankcard,
                    ];
                    Db::name("user_proxy")->where("up_id", $up_id)->update($base_info);
                }
                if(is_numeric($upc_id)){ # 修改
                    $buy_info = [
                        "buy_level" => $buy_level,
                        "credit_level" => $credit_level,
                        "judg_level" => $judg_level,
                        "fee_rate" => $fee_rate * 100,
                        "remark" => $remark,
                    ];
                    Db::name("user_proxy_company")->where("upc_id", $upc_id)->update($buy_info);
                    Code(200, "代拍信息修改成功");
                }else{ # 新增
                    $buy_info = [
                        "up_id" => $up_id,
                        "aid" => $aid,
                        "buy_level" => $buy_level,
                        "credit_level" => $credit_level,
                        "judg_level" => $judg_level,
                        "fee_rate" => $fee_rate * 100,
                        "remark" => $remark,
                        "reg_time" => time(),
                    ];
                    Db::name("user_proxy_company")->insert($buy_info);
                    Code(200, "代拍信息修改成功");
                }
            }
        }else{
            Code(403, "服务器错误，请稍后再试");
        }
    }

}
